<?php 

require_once("form/test.php");

class IntegerForm extends Form {
	
	public function __construct(){
		parent::__construct(array(
			"age" => new IntegerField(),
		));
	}
	
}

class IntegerFieldTest extends Test {
	
	public function run(){
		$form = new IntegerForm();
		$this->tags($form);
		$this->invalid($form);
		$this->valid($form);
		$this->blank($form);
		$this->text($form);
	}
	
	private function tags($form){
		$this->assertEquals($form->render("age"), "<input type=\"text\" name=\"age\" id=\"age\" value=\"\" />");
		$form->populate(array("age" => 25));
		$this->assertEquals($form->render("age"), "<input type=\"text\" name=\"age\" id=\"age\" value=\"25\" />");
		$form->clean();
		$form->populate(array("age" => "testando"));
		$this->assertEquals($form->render("age"), "<input type=\"text\" name=\"age\" id=\"age\" value=\"\" />");
	}
	
	private function invalid($form){
		$this->assertEquals(FALSE, $form->isValid(), "Form is not valid");
		$this->assertTrue(in_array("age", $form->errors), "Age contains error");
		$this->assertTrue($form->containsError("age"), "Age contains error");
		$this->assertEmpty($form->getValue("age"));
		$form->clean();
	}
	
	private function valid($form){
		$form->populate(array("age" => "25"));
		$this->assertTrue($form->isValid(), "Form valid");
		$this->assertFalse(in_array("age", $form->errors), "Age does NOT contain error cause it's valid");
		$this->assertFalse($form->containsError("age"), "Age does NOT contain error cause it's valid");
		$this->assertNotEmpty($form->getValue("age"));
		$this->assertEquals($form->getValue("age"), 25);
		$form->clean();
	}
	
	private function blank($form){
		$form->populate(array("age" => "    "));
		$this->assertEquals(FALSE, $form->isValid(), "Form is not valid");
		$this->assertTrue(in_array("age", $form->errors), "Age contains error");
		$this->assertTrue($form->containsError("age"), "age contains error");
		$this->assertEmpty($form->getValue("age"));
		$form->clean();
	}
	
	private function text($form){
		$form->populate(array("age" => "text"));
		$this->assertEquals(FALSE, $form->isValid(), "Form is not valid");
		$this->assertTrue(in_array("age", $form->errors), "Age contains error");
		$this->assertTrue($form->containsError("age"), "age contains error");
		$this->assertEmpty($form->getValue("age"));
		$form->clean();
	}
	
}

$test = new IntegerFieldTest();
$test->run();